package com.health.mapp.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.health.db.model.GoodsDataStuffRemark;
import com.health.db.model.GoodsDataUser;
import com.health.db.service.sm.IGoodsDataService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.UpdateHealthRecordReq;
import com.health.mapp.bean.UpdateHealthRecordResp;
import com.health.mapp.utils.DateUtils;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 * @author HiRoad
 *
 */
public class UpdateHealthRecordImpl implements ActionInterface {
	
	private IGoodsDataService goodsDataService ;
	
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		
		UpdateHealthRecordReq req=(UpdateHealthRecordReq) reqBean;
		UpdateHealthRecordResp _resp= new UpdateHealthRecordResp();
		
		//工单Id
		String recId= req.getRecId();
		//医生编号
		String docId =req.getDocId();
		
		String subTitle= req.getSubTitle();
			
		String subDesc = req.getSubDesc();
		
		String defaultFlag =  req.getDefaultFlag();
		 		
		 //医生编号 用户编号 订单号 取一
		 if(StringUtils.isNotBlank(recId)){
			 
			 List<GoodsDataStuffRemark> list = new ArrayList<GoodsDataStuffRemark>();
			 GoodsDataUser dataUser =  goodsDataService.selectGoodsDataUser(Long.valueOf(recId));
			 GoodsDataUser dataUserUpdate = new GoodsDataUser();
			 dataUserUpdate.setAccepterId(Long.valueOf(docId));
			 dataUserUpdate.setAccepterName(req.getDocName());
			 dataUserUpdate.setRecid(dataUser.getRecid());
			 
			 if (StringUtils.isNotBlank(subTitle)&&StringUtils.isNotBlank(subTitle)) {
				 //数组已分隔符结尾会少算数量
				 if (subTitle.endsWith("#")) {
					 subTitle = subTitle+" ";
				 }
				 if (subDesc.endsWith("#")) {
					 subDesc = subDesc+" ";
				 }
				 String[] subTitles=subTitle.split("#");
					
				 String[] subDescs =subDesc.split("#");
				 
				 String[] defaultFlags =defaultFlag.split("#");
				
				 if (subTitles.length!=subDescs.length&&subTitles.length!=defaultFlags.length) {
					_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
					_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
					return _resp;
				 }
				 
				 Date now  =DateUtils.now();
				 for (int i = 0; i < subTitles.length; i++) {
					 
					 if (StringUtils.isBlank(subTitles[i])&&StringUtils.isBlank(subDescs[i])){
							continue;
						}
					 GoodsDataStuffRemark remarkTemp = new GoodsDataStuffRemark();
					 remarkTemp.setGoodsDataId(dataUser.getGoodsDataId());
					 remarkTemp.setGoodsDataName(dataUser.getItemName());
					 remarkTemp.setUserDataId(dataUser.getRecid());
					 remarkTemp.setSubTitle(subTitles[i]);			
					 remarkTemp.setSubDesc(subDescs[i]);
					 
					 if (defaultFlags[i].equals("1")) {
						 remarkTemp.setDefaultFlag((byte) 1);
					}else{
						 remarkTemp.setDefaultFlag((byte) 0);
					}
					 remarkTemp.setUpdateDate(now);
					 remarkTemp.setUpdateUserid(Long.valueOf(docId));
					 list.add(remarkTemp);
				}
			 }
			 goodsDataService.updateGoodsDataUser(dataUserUpdate, list);
			 
		}else {
			_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return _resp;
		}
		_resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
		_resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);
 
		return _resp;
	}

	public IGoodsDataService getGoodsDataService() {
		return goodsDataService;
	}

	public void setGoodsDataService(IGoodsDataService goodsDataService) {
		this.goodsDataService = goodsDataService;
	}
	
}
